//  Copyright (c) Microsoft Corporation.  All Rights Reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace Client
{
    class Program
    {   
        static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCert);

            CalculatorClient client = new CalculatorClient();
            // WARNING: The below is not suitable for production code. Passwords should not
            // be embedded in code and production code should have a UI or other mechanism 
            // to get the password from the user.
            client.ClientCredentials.UserName.UserName = "alice";
            client.ClientCredentials.UserName.Password = "ecila";
            Console.WriteLine("Client is ready. Press [Enter] to invoke Service.");
            Console.ReadLine();

            // Call the Add service operation
            double value1 = 100.00D;
            double value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

            // Closing the client gracefully closes the connection and cleans 
            // up resources
            client.Close();

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }

        // WARNING: The below certificate validation code is not suitable for production code.
        // This is a simple SSL certificate validation code for demonstration purpose only.
        static bool ValidateServerCert(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
        {
            Console.WriteLine("Validating SSL certificate.");

            if (cert.Subject != "CN=ClientSTS")
            {
                Console.WriteLine("Untrusted STS Certificate.");
                return false;
            }

            Console.WriteLine("Trusted STS Certificate.");
            Console.WriteLine("Cert subject name: {0}", cert.Subject);

            return true;
        }
    }
}
